package com.ruoyi.common.flowable;

import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.service.FlowInstanceExtServiceImpl;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.engine.delegate.event.impl.FlowableEntityWithVariablesEventImpl;
import org.flowable.task.service.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

/**
 * 任务节点前置监听处理类
 * @author: Lu Yang
 * @create: 2019-05-04 20:51
 **/
@Component
public class TaskCompletedListener implements FlowableEventListener {

    public FlowInstanceExtServiceImpl getFlowInstanceExtServiceImpl() {
        return SpringUtils.getBean(FlowInstanceExtServiceImpl.class);
    }
    @Override
    public void onEvent(FlowableEvent event) {

        // 当前节点任务实体
         if(event instanceof FlowableEntityEventImpl ){
            TaskEntity taskEntity = (TaskEntity) ((FlowableEntityEventImpl) event).getEntity();
            // TODO 获取到了taskEntity 自己做每个节点的前置操作
            System.out.println("1111111111");
             getFlowInstanceExtServiceImpl().updateExt(taskEntity.getProcessInstanceId());
        }else if(event instanceof FlowableEntityWithVariablesEventImpl){
            TaskEntity taskEntity = (TaskEntity) ((FlowableEntityWithVariablesEventImpl) event).getEntity();

             getFlowInstanceExtServiceImpl().updateExt(taskEntity.getProcessInstanceId());
            // TODO 获取到了taskEntity 自己做每个节点的前置操作
            System.out.println("1111111111" + event);

        }

    }

    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}